package com.rockwell.sniffyhunter.model;

import org.opencv.core.Point;

import com.rockwell.sniffyhunter.utils.IDetector;

public class MazeDetector implements IDetector {

	private boolean m_present = false;
	private Point m_center;
	private float m_radius;
	private int m_width = 0;
	private int m_height = 0;
	
	@Override
	synchronized public void onDetect(boolean present, Point center, float radius, int height, int width) {
		if (present && center.y < height * FOV_LOWER_HEIGHT_BOUNDARY_MAZE)
			present = false;
		
		if (present && center.y > height * FOV_UPPER_WIDTH_BOUNDARY_MAZE)
			present = false;
		
		if (present && center.x < width * FOV_LOWER_WIDTH_BOUNDARY_MAZE)
			present = false;
		
		if (present && center.x > width * FOV_UPPER_WIDTH_BOUNDARY_MAZE)
			present = false;
		
		if (!this.m_present && !present)
			return;
		
		if (this.m_present && present)
		{
			double difX = Math.abs(m_center.x - center.x);
			double difY = Math.abs(m_center.y - center.y);
			
			if (difX < 1 && difY < 1)
				return;
		}
		
		m_present = present;
		m_center = center;
		m_radius = radius;
		
		if (m_width != width)
			m_width = width;
		
		if (m_height != height)
			m_height = height;
	}
	
	@Override
	public boolean isPresent() {
		return m_present;
	}
	
	@Override
	public Point getDrift() {
		double driftX = m_center.x - m_width * OBSTACLE_CENTER_POSITION_X;
		double driftY = m_height * OBSTACLE_CENTER_POSITION_Y - m_center.y;
		
		if (Math.abs(driftX) < m_height * OBSTACLE_CENTER_TOLERANCE)
			driftX = 0;
		if (Math.abs(driftY) < m_height * OBSTACLE_CENTER_TOLERANCE)
			driftY = 0;
		
		return new Point(driftX, driftY);
	}
	
	public float getObstacleRadius() {
		return m_radius;
	}

	@Override
	public Point getCenter() {
		return m_center;
	}

}
